package cn.tedu.ximart.server.controller;

import cn.tedu.ximart.server.pojo.dto.BrandAddNewDTO;
import cn.tedu.ximart.server.pojo.dto.BrandEditDTO;
import cn.tedu.ximart.server.pojo.vo.BrandListItemVO;
import cn.tedu.ximart.server.service.IBrandService;
import cn.tedu.ximart.server.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;


@Slf4j
@Api(tags = "品牌管理模块")
@RestController
@RequestMapping("/brands")
public class BrandController {


    @Autowired
    public IBrandService brandService;

    public BrandController(){
        log.debug("BrandController的构造方法被调用了");
    }

    @ApiOperation("增加品牌")
    @ApiOperationSupport(order = 100)
    @PostMapping("/add-new")
    @PreAuthorize("hasAuthority('/pms/product/update')")
    public JsonResult addNew(@RequestBody @Valid BrandAddNewDTO brandAddNewDTO){
        log.debug("BrandController.addNew");
        log.debug("接收到的请求参数：{}",brandAddNewDTO);
        brandService.addNew(brandAddNewDTO);
        return JsonResult.ok();
    }

    @ApiOperation("查询品牌列表")
    @ApiOperationSupport(order = 400)
    @PreAuthorize("hasAnyAuthority('/pms/product/delete','/pms/product/read','/pms/product/update')")
    @GetMapping("")
    public JsonResult list(){
        log.debug("接收到查询品牌列表的请求");
        List<BrandListItemVO> brands = brandService.list();
        return JsonResult.ok(brands);
    }


    @ApiOperation("删除品牌")
    @ApiOperationSupport(order = 200)
    @PostMapping("/{id:[0-9]+}/delete")
    @PreAuthorize("hasAuthority('/pms/product/delete')")
    public JsonResult delete(@PathVariable Long id){
        log.debug("BrandController.delete{}",id);
        brandService.deleteById(id);
        return JsonResult.ok();
    }


    @ApiOperation("根据id编辑品牌")
    @ApiOperationSupport(order = 300)
    @PostMapping("/{id:[0-9]+}/edit")
    @PreAuthorize("hasAuthority('/pms/product/update')")
    public String edit(@PathVariable Long id , BrandEditDTO brandEditDTO){
        log.debug("BrandController.edit");
        log.debug("{}",brandEditDTO);
        return "尝试修改品牌信息";
    }

    @ApiOperation("根据name修改品牌")
    @ApiOperationSupport(order = 301)
    @PostMapping("/{name:[a-zA-Z]+}/edit")
    @PreAuthorize("hasAuthority('/pms/product/update')")
    public String edit(@PathVariable String name,BrandEditDTO brandEditDTO){
        log.debug("BrandController.edit{}",name,brandEditDTO);
        return "尝试修改商品信息";
    }



}
